package org.unicat.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.unicat.mapper.TeacherMapper;
import org.unicat.mapper.UserInfoMapper;
import org.unicat.mapper.UserMapper;
import org.unicat.model.Teacher;
import org.unicat.model.User;
import org.unicat.model.UserInfo;
import org.unicat.service.TeacherService;
import org.unicat.service.UserRegistService;
import org.unicat.service.UserService;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserRegistService userRegistService;


    @Override
    public List<Teacher> findAllTeacher() {
        return teacherMapper.selectAll();
    }

    @Override
    public Teacher findTeacherByName(String name) {
        return teacherMapper.selectOne(name);
    }

    @Override
    public UserInfo getTearcherInfo(Teacher teacher) {
        UserInfo teacherInfo = userInfoMapper.selectOne(teacher.getName());
        teacherInfo.setPassword(userMapper.selectOne(teacher.getName()).getPassword());
        teacherInfo.setMajor(teacherMapper.selectOne(teacher.getName()).getMajor());
        return teacherInfo;
    }

    @Override
    public void addTeacher(UserInfo teacher) {
        userRegistService.teacherRegist(teacher);
    }

    @Override
    public void updateTeacherInfo(UserInfo teacher) {
        //更新user表
        User user = new User();
        user.setId(userMapper.selectOne(teacher.getUsername()).getId());
        user.setUsername(teacher.getUsername());
        user.setPassword(teacher.getPassword());
        userMapper.updateUser(user);
        //更新信息表
        teacher.setId(userInfoMapper.selectOne(teacher.getUsername()).getId());
        userInfoMapper.updateUserInfo(teacher);
        //更新Teacher表
        Teacher teacher1 = new Teacher();
        teacher1.setId(teacherMapper.selectOne(teacher.getUsername()).getId());
        teacher1.setName(teacher.getUsername());
        teacher1.setMajor(teacher.getMajor());
        teacherMapper.updateTeacher(teacher1);
    }

    @Override
    public void deleteTeacher(Teacher teacher) {
        teacherMapper.deleteTeacher(teacher);
        userInfoMapper.deleteUserInfo(userInfoMapper.selectOne(teacher.getName()));
        userMapper.deleteUser(userMapper.selectOne(teacher.getName()));
    }
}
